//
//  ResultadosBusquedaViewController.m
//  FinApp
//
//  Created by Marcel Arbó Lack on 11/11/11.
//  Copyright (c) 2011 FIB-Fàcultat d'informàtica de Barcelona. All rights reserved.
//

#import "ResultadosBusquedaViewController.h"
#import "ResultatsMapViewController.h"
#import "ResultatsListViewController.h"
#import "AppDelegate.h"
#import "ListFlat.h"
#import "DetailViewController.h"

@interface ResultadosBusquedaViewController()
- (void)buildSegmented;
- (void)setBackBarButtonItemNewLocalizedText;
@end

@implementation ResultadosBusquedaViewController

@synthesize navigationBar = _navigationBar;
@synthesize containerView = _containerView;
@synthesize segmentedControl = _segmentedControl;
@synthesize flats = _flats;
@synthesize mapVC = _mapVC;
@synthesize listVC = _listVC;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buildSegmented];
    [self setBackBarButtonItemNewLocalizedText];
    
    
    _mapVC = [[ResultatsMapViewController alloc] initWithNibName:@"ResultatsMapViewController" bundle:nil];
    _listVC = [[ResultatsListViewController alloc] initWithNibName:@"ResultatsListViewController" bundle:nil];
    
    [_mapVC setParentVC:self];
    [_listVC setParentVC:self];
    [_containerView addSubview:_mapVC.view];
    [_mapVC loadMap];
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    
    [_navigationBar release];
    
    [_containerView release];
    
    [_segmentedControl release];
    [super dealloc];
    
}


- (void)showSubviews:(id)sender {
    UISegmentedControl *segmented = (UISegmentedControl*)sender;
    if ([segmented selectedSegmentIndex] == 0) {
        for (UIView * view in _containerView.subviews) {
            [view removeFromSuperview];
        }
        [_containerView addSubview:_mapVC.view];
        [_mapVC loadMap];
    } else {
        for (UIView * view in _containerView.subviews) {
            [view removeFromSuperview];
        }
        [_containerView addSubview:_listVC.view];
    }
    
}
#pragma mark - Private Methods

- (void)setBackBarButtonItemNewLocalizedText {
    
    _navigationBar.topItem.leftBarButtonItem = nil;
    
    //Set BackBarButton 
    UIButton *customBackButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 37)];
    [customBackButton setBackgroundImage:[UIImage imageNamed:@"comun_btn_back.png"] forState:UIControlStateNormal];
    [customBackButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 0, 78, 35)];
    [backLabel setText:NSLocalizedString(@"_atras", @"")];
    [backLabel setTextAlignment:UITextAlignmentCenter];
    [backLabel setTextColor:[UIColor whiteColor]];
    [backLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
    [backLabel setBackgroundColor:[UIColor clearColor]];
    
    UIView *customBackButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 37)];
    [customBackButtonView addSubview:customBackButton];
    [customBackButtonView addSubview:backLabel];
    [customBackButton release];
    [backLabel release];
    
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customBackButtonView];
    [_navigationBar.topItem setLeftBarButtonItem:backBarButtonItem];
    [customBackButtonView release];
    [backBarButtonItem release];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) buildSegmented {
    //Add mySegmentedControl to navigationBar
    NSArray *segmentedItemsArray = [[NSArray alloc] initWithObjects:NSLocalizedString(@"_mapa",@""), NSLocalizedString(@"_llistat",@""), nil];
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedItemsArray];
    [segmentedItemsArray release];
    _segmentedControl.frame = CGRectMake(0, 0, 146, 30);
    _segmentedControl.tintColor = [UIColor blackColor];
    _segmentedControl.center = _navigationBar.center;
    _segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
    _segmentedControl.selectedSegmentIndex = 0;
    [_segmentedControl addTarget:self action:@selector(showSubviews:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentedControl];
}

- (void) goToDetail:(ListFlat*)flat {
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    [detailVC setFlat:(Flat*)flat];
    [detailVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}

@end
